/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.facades.persistence;

import com.sdd.entities.Category;
import com.sdd.entities.Product;
import com.sdd.entities.Product_;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Root;

/**
 *
 * @author ceu
 */
@Stateless
public class ProductPersistenceFacade extends AbstractPersistenceFacade<Product>{

    @PersistenceContext(unitName = "SDDStore-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ProductPersistenceFacade() {
        super(Product.class);
    }
    

    @Override
    public List<Product> findAll() {
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Product> query = builder.createQuery(Product.class);
        Root<Product> r = query.from(Product.class);
        query.select(r).where(builder.isNull(r.get(Product_.dateRemoved)));
        //query.select(r).where();
        return getEntityManager().createQuery(query).getResultList();
    }
    
    @Override
    public List<Product> findRange(int[] range) {
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Product> query = builder.createQuery(Product.class);
        Root<Product> r = query.from(Product.class);
        query.select(r).where(builder.isNull(r.get(Product_.dateRemoved)));
        return getEntityManager().createQuery(query).setMaxResults((range[1] - range[0])).setFirstResult(range[0]).getResultList();
    }
    
    public List<Product> getProductsByCategory(Category c){
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Product> query = builder.createQuery(Product.class);
        Root<Product> r = query.from(Product.class);
        query.select(r).where(builder.and(builder.isMember(c, r.get(Product_.categories)),builder.isNull(r.get(Product_.dateRemoved))));
        //query.select(r).where();
        return getEntityManager().createQuery(query).getResultList();
    }
    
    public List<Product> getProductsWithoutCategory(){
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Product> query = builder.createQuery(Product.class);
        Root<Product> r = query.from(Product.class);
        query.select(r).where(builder.and(builder.isEmpty(r.get(Product_.categories)),builder.isNull(r.get(Product_.dateRemoved))));
        //query.select(r).where();
        return getEntityManager().createQuery(query).getResultList();
    }
    
    public List<Product> getProductsWithoutCategory(int range[]){
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Product> query = builder.createQuery(Product.class);
        Root<Product> r = query.from(Product.class);
        query.select(r).where(builder.and(builder.isEmpty(r.get(Product_.categories)),builder.isNull(r.get(Product_.dateRemoved))));
        return getEntityManager().createQuery(query).setMaxResults((range[1] - range[0])).setFirstResult(range[0]).getResultList();
    }
    
    public Product disable(Product p){
        p.setDateRemoved(new Date());
        System.out.println("EN DISABLE: "+p);
        return getEntityManager().merge(p);
    }
    
      
}
